package com.weblog.search.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Feign拦截器，解决头信息丢失问题
 * @author MaoLin Wang
 * @date 2019/9/2217:04
 */
@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes!=null){
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader("Authorization");
            requestTemplate.header("Authorization",token);
        }else {
            System.out.println("FeignInterceptor-----------attributes为空");
        }

      /*  Enumeration<String> headerNames = request.getHeaderNames();
        System.out.println(headerNames);
        if(headerNames!=null){
            while (headerNames.hasMoreElements()){
                String name=headerNames.nextElement();
                String value=request.getHeader(name);
                requestTemplate.header(name,value);
            }
        }*/


    }
}
